/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itesm.daai.xao;

/**
 *
 * @author Karina
 */



import java.io.*;
import org.exist.xmldb.XQueryService;
import org.xmldb.api.*;
import org.xmldb.api.base.*;

public class XAO {
        /**
     *
     * XAODBQuery
     *
     * Se hace la conexion el Exist y regresa los datos en formato XML
     *
     * @return String regresa los datos en formato XML
     * @throws java.lang.Exception
     */

    
    public String XAODBQuery(String driver, String conexion) throws Exception {
        Resource r=null;
        //String driver = "org.exist.xmldb.DatabaseImpl";
        Class cl = Class.forName(driver);
        Database database = (Database)cl.newInstance();
        DatabaseManager.registerDatabase(database);
        Collection col = (Collection) DatabaseManager.getCollection("conexion");
        System.out.println("Se obtuvo coleccion");
        XQueryService service = (XQueryService) col.getService("XPathQueryService", "1.0");
        service.setProperty("indent", "yes");
        try{
            ResourceSet result = service.query("/noticiasccm");
            ResourceIterator i = result.getIterator();
            System.out.println("Sacando recursos");
            while(i.hasMoreResources()) {

                r = i.nextResource();
                System.out.println(r);
            }
        } catch(Exception e){
            System.out.println("Error al leer el archivo xml");
        }
        System.out.println((String)r.getContent());
       return (String)r.getContent();

    }

}
